<?php
/**
 * 开发商数据库（租户库）的Entity基础类
 * Created by PhpStorm.
 * User: wangx08
 * Date: 2015-03-28
 * Time: 16:58
 */

namespace common\entities;

class KfsDbEntity extends BaseEntity {

    static $db = null;

    static $dbConnection = null;

    static $orgcode = null;

    /**
     * @return \yii\db\Connection
     * @throws \yii\base\InvalidConfigException
     */
    public static function getDb(){

        if(is_null(self::$db)) {
            if(!is_null(self::$dbConnection)){
                $connection = self::$dbConnection;
            }
            else {
                $connection = \Yii::$app->user->identity->kfsDb;
            }
            $db = new \yii\db\Connection([
                'dsn' => 'mysql:host=' . $connection['server'] . ';dbname=' . $connection['database'],
                'username' => $connection['uid'],
                'password' => $connection['pwd'],
                'charset' => 'utf8'
            ]);
            self::$db = $db;
        }
        return self::$db;
    }

    /**
     * 直接设置特定租户库
     * 
     * @param \yii\db\Connection $connection 
     */
    public static function setDb(\yii\db\Connection $connection)
    {
        self::$db = $connection;
    }
}